package com.slz3.business.module.goodsManagement.gooInventory.domain;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.LocalDT;
import com.slz3.business.frame.util.UUIDUtils;
import com.slz3.business.frame.util.VE;
import org.springframework.util.MultiValueMap;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.stream.Collectors;

public class GooInventoryFactory {

    public static final String DT_OF_PATTERN = "yyMMddHHmmssSSS";

    private static final String CODE = "N";

    private static String generateNumber() {
        return CODE.concat(LocalDT.getDateTimeAsString(LocalDateTime.now(), DT_OF_PATTERN))
                .concat(String.valueOf((int) (Math.random() * (10))));
    }

    /**
     * 初始化基础属性值
     */
    public static GooInventory initBaseFields(GooInventory gooInventory) {
        if (gooInventory.getUuid() == null) {
            gooInventory.setUuid(UUIDUtils.generateUUID());
            gooInventory.setSerNum(GooInventoryFactory.generateNumber());
            gooInventory.setCreateTime(LocalDateTime.now());
            if (VE.isEmpty(gooInventory.getSerAudit())) {
                gooInventory.setSerAudit(AuditFlag.WAIT_SUBMIT);
            }
            if (VE.isEmpty(gooInventory.getSerUsing())) {
                gooInventory.setSerUsing(UsingFlag.ENABLE);
            }
        } else {
            gooInventory.setLastModifyTime(LocalDateTime.now());
        }
        return gooInventory;
    }

    /**
     * 初始化基础属性值
     */
    public static GooInventory initBaseFields() {
        return initBaseFields(newTemStyle());
    }

    public static GooInventory newTemStyle() {
        return new GooInventory();
    }

    public static GooInventoryCondition newGooInventoryCondition(MultiValueMap<String, String> params) {

        GooInventoryCondition condition = new GooInventoryCondition();

        String createTimeOUTSET = params.getFirst("createTimeOUTSET");
        String createTimeFINISH = params.getFirst("createTimeFINISH");
        String lastModifyTimeOUTSET = params.getFirst("lastModifyTimeOUTSET");
        String lastModifyTimeFINISH = params.getFirst("lastModifyTimeFINISH");
        String serAuditList = params.getFirst("serAuditList");

        condition.setCreateTimeOUTSET(VE.isEmpty(createTimeOUTSET) ? null : LocalDT.parseStringToDateTime(createTimeOUTSET, GooInventoryCondition.CREATE_TIME_OF_PATTERN));
        condition.setCreateTimeFINISH(VE.isEmpty(createTimeFINISH) ? null : LocalDT.parseStringToDateTime(createTimeFINISH, GooInventoryCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeOUTSET(VE.isEmpty(lastModifyTimeOUTSET) ? null : LocalDT.parseStringToDateTime(lastModifyTimeOUTSET, GooInventoryCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeFINISH(VE.isEmpty(lastModifyTimeFINISH) ? null : LocalDT.parseStringToDateTime(lastModifyTimeFINISH, GooInventoryCondition.CREATE_TIME_OF_PATTERN));
        condition.setSerAuditList(VE.isEmpty(serAuditList) ? null : Arrays.stream(serAuditList.split(",")).map(item -> AuditFlag.valueOf(item)).collect(Collectors.toList()));

        return condition;

    }

    public static GooInventory newByAuditAuditStatus(GooInventory domain) {
        GooInventory inventory = new GooInventory();
        Steward.merge(domain, inventory);
        inventory.setSerAudit(AuditFlag.APPROVED);
        GooInventoryFactory.initBaseFields(inventory);
        return inventory;
    }

    public static GooInventory newBySerAmountCurrentTotal(GooInventory domain, Long serAmountCurrentTotal) {
        if (serAmountCurrentTotal == null) {
            return null;
        }
        GooInventory inventory = new GooInventory();
        Steward.merge(domain, inventory);
        inventory.setSerAmountCurrentTotal(serAmountCurrentTotal);
        GooInventoryFactory.initBaseFields(inventory);
        return inventory;
    }

}
